<script>
import { save_clients } from "./utils/imapi";
export default {
  onLaunch: function () {
    console.log("App Launch");
    //#ifdef APP-PLUS
    plus.screen.lockOrientation("portrait-primary");
    const that = this;
    console.log(that);
    setTimeout(async () => {
      const clientInfo = plus.push.getClientInfo();
      console.log(clientInfo.clientid);
      if (JSON.stringify(clientInfo.clientid) == "null") {
        plus.device.getOAID({
          success: (e) => {
            console.log("getOAID success: " + JSON.stringify(e));
            this.globalData.cid = e.oaid;
          },
          fail: (e) => {
            this.error = JSON.stringify(e);
            console.log("getOAID failed: " + JSON.stringify(e));
          },
        });
      } else {
        this.globalData.cid = clientInfo.clientid;
      }
      // // 保存cid

      // try {
      //   const data = await save_clients({
      //     cid:this.globalData.cid,
      //     platform:'mch'
      //   })
      //   console.log(data);
      // } catch (error) {

      // }
    }, 1000);
    uni.getSystemInfo({
      success: (e) => {
        console.log(e);
        that.globalData.screenHeight = e.screenHeight;
        that.globalData.screenWidth = e.screenWidth;
        that.globalData.StatusBar = e.statusBarHeight;
      },
    });
    // 监听离线点击消息事件
    plus.push.addEventListener(
      "click",
      function (msg) {
        console.log("1111");
        console.log(msg);
        console.log(plus.os.name);
        var payload = msg.payload;
        console.log(payload);
        if (msg.payload.type) {
          const type = msg.payload.type;
          console.log(type);
          switch (type) {
            case "cutpriceApprove":
              setTimeout(() => {
                console.log("1111");
                uni.navigateTo({
                  url: "/pages/bargin_twitter/bargin_twitter",
                });
              }, 1000);
              break;

            default:
              break;
          }
        }
      },
      true
    );
    plus.push.addEventListener(
      "receive",
      function (msg) {
        console.log(msg);
        if (msg.payload) {
          if (typeof msg.payload == "string") {
            uni.showToast({
              title: "msg.payload" + msg.payload,
              duration: 2000,
            });
            // alert( "payload(String): "+msg.payload );
          } else {
            var id = msg.payload.id;
            // var url = "www/tpl/detail.html?id=" + id;
            plus.nativeUI.confirm(
              "收到一条新消息，是否立即查看",
              function (e) {
                if (e.index == 0) {
                  uni.navigateTo({
                    url: "/pages/order_list/order_list?id=" + id,
                  });
                }
              },
              "新消息通知",
              ["查看", "忽略"]
            );
          }
        } else {
          //alert( "payload: undefined" );
        }
      },
      false
    );
    //#endif
    try {
      const token = uni.getStorageSync("token");
      console.log(token);
      if (token) {
        const date = new Date().getTime() / 1000;
        const expre_time = uni.getStorageSync("expre_time");
        console.log(date);
        console.log(expre_time);
        if (date > expre_time) {
          uni.navigateTo({
            url: "/pages/login/login",
          });
        }
      } else {
        uni.navigateTo({
          url: "/pages/login/login",
        });
      }
    } catch (error) {
      uni.navigateTo({
        url: "/pages/login/login",
      });
    }
  },
  onShow: function () {
    console.log("App Show");
  },
  onHide: function () {
    console.log("App Hide");
  },
  globalData: {
    StatusBar: "",
    cid: "",
    token: "",
    userinfo: {},
  },
};
</script>

<style lang="scss">
@import url("./app.css");
@import "uview-ui/index.scss";
@import "./style/iconFont.css";
/*每个页面公共css */

</style>
